import useTokenStore from "@/stores/token"
import request, { wrap_api_func } from "./request"

const BASE_URL = "/api/v1/assistant"

export const llm_factories_list = wrap_api_func(async () => {
    const { data } = await request.get(`${BASE_URL}/llm/list`)
    return {
        code: data.code,
        msg: data.msg,
        data: data.data ? {
            version: data.data.version,
            factory_llm_infos: data.data.factory_llm_infos
        } : null
    }
})

export const list_assistant_metas = wrap_api_func(async () => {
    const { data } = await request.get(`${BASE_URL}/meta/list`, {
        headers: useTokenStore().getAuthHeader()
    })
    return {
        code: data.code,
        msg: data.msg,
        data: data.data ? {
            metas: data.data.metas
        } : null
    }
})

export const create_assistant = wrap_api_func(async ({assistant_setting, model_setting}) => {
    const { data } = await request.post(`${BASE_URL}/create`, {
        assistant_setting,
        model_setting
    }, {
        headers: useTokenStore().getAuthHeader()
    })
    return {
        code: data.code,
        msg: data.msg,
        data: data.data ? {
            id: data.data.id,
            name: data.data.name,
            lastUseAt: data.data.lastUseAt
        } : null
    }
})

export const update_assistant = wrap_api_func(async ({ assistantId, assistant_setting, model_setting }) => {
    const { data } = await request.post(`${BASE_URL}/${assistantId}/update`, {
        assistant_setting: assistant_setting,
        model_setting: model_setting
    }, {
        headers: useTokenStore().getAuthHeader()
    })
    return {
        code: data.code,
        msg: data.msg,
        data: data.data ? {
            id: data.data.id,
            name: data.data.name,
            lastUseAt: data.data.lastUseAt
        } : null
    }
})

export const get_assistant = wrap_api_func(async ({assistantId}) => {
    const { data } = await request.get(`${BASE_URL}/${assistantId}/get`, {
        headers: useTokenStore().getAuthHeader()
    })
    return {
        code: data.code,
        msg: data.msg,
        data: data.data ? {
            id: data.data.id,
            assistant_setting: data.data.assistant_setting,
            model_setting: data.data.model_setting
        } : null
    }
})

export const delete_assistant = wrap_api_func(async ({assistantId}) => {
    const { data } = await request.post(`${BASE_URL}/${assistantId}/delete`, null, {
        headers: useTokenStore().getAuthHeader()
    })
    return {
        code: data.code,
        msg: data.msg,
        data: data.data ? {
            id: data.data.id,
            name: data.data.name,
            lastUseAt: data.data.lastUseAt
        } : null
    }
})